<template>
  <el-dialog v-model="visible" width="600px"  draggable align-center>
    <el-form ref="formRef" :model="formData" :rules="formRules" class="p-2" label-width="120px">
      <el-form-item label="账号" prop="accountNo">
        <el-input v-model="formData.accountNo"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password"></el-input>
      </el-form-item>
      <el-form-item label="用户名称" prop="username">
        <el-input v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-input v-model="formData.userType" disabled></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="confirm" :loading="confirmLoading">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ElMessage } from "element-plus";


const visible = ref(false)

const type = ref('')
const formRef = ref()
const formData = ref({})
const formRules = ref({
  accountNo: [{required: true, message: '账号不能为空', trigger: ['change']}],
  password: [{required: true, message: '密码不能为空', trigger: ['change']}],
  username: [{required: true, message: '用户名称不能为空', trigger: ['change']}],
  userType: [{required: true, message: '用户类型不能为空', trigger: ['change']}],
})


import request from '@/config/axios'

const confirmLoading = ref(false)

const emit = defineEmits(["ok"])
const confirm = async function () {
  await formRef.value?.validate().then((valid) => {
    if (!valid) {
      return
    }
  })
  confirmLoading.value = true
  console.log("--formData----",formData.value)
  try {
    await request.post({
      url: '/userManage/save',
      data: formData.value
    })
  } catch (error) {
    throw error;
  } finally {
    confirmLoading.value = false
    emit('ok')
  }
  visible.value = false
  ElMessage.success("保存成功!")
}





defineExpose({
  add: () => {
    type.value = 'add'
    formData.value = {}
    formData.value.userType = 3
    visible.value = true
  }
})

</script>

<style scoped lang="scss"></style>
